package com.fxkj.cloud.drms.infra.sql.TDengine;

import com.fxkj.cloud.dbs.common.model.kv.TsKvEntry;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.Data;

import java.sql.PreparedStatement;
import java.util.List;
import java.util.concurrent.Executor;

@Data
public class TDEngineResultSet<T> {


    private final PreparedStatement preStmt;

    private final ListenableFuture<T> executeFuture;


    public TDEngineResultSet(PreparedStatement preStmt, ListenableFuture<T> executeFuture) {
        this.preStmt = preStmt;
        this.executeFuture = executeFuture;
    }

    public ListenableFuture<Boolean> booleanResult(Executor executor, Function<T, Boolean> transform) {
        ListenableFuture<Boolean> future = Futures.transform(executeFuture, transform, executor != null ? executor : MoreExecutors.directExecutor());
        return future;
    }

    public ListenableFuture<List<TsKvEntry>> tskvEntriesResult(Executor executor, Function<T, List<TsKvEntry>> transform){
        ListenableFuture<List<TsKvEntry>> future = Futures.transform(executeFuture, transform, executor != null ? executor : MoreExecutors.directExecutor());
        return future;
    }

    public ListenableFuture<TsKvEntry> tskvEntryResult(Executor executor, Function<T, TsKvEntry> transform){
        ListenableFuture<TsKvEntry> future = Futures.transform(executeFuture, transform, executor != null ? executor : MoreExecutors.directExecutor());
        return future;
    }

    public <S> ListenableFuture<S> transform(Executor executor, Function<T, S> transform){
        ListenableFuture<S> future = Futures.transform(executeFuture, transform, executor != null ? executor : MoreExecutors.directExecutor());
        return future;
    }




}
